#include "search.h"

//顺序查找
//传递地址值给指针才能指向原来的对象（引用传递）
int sequenceSearch(SSTable *table, Element *key) {
    if (table == nullptr || key == nullptr || table->length == 0) {
        return -1;
    }

    Element *element = table->element;
    int i = 0;
    while (i < table->length) {
        if (key->num == element[i].num) {
            return i;
        }
        i++;
    }
    if (i == table->length) {
        return -1;
    }
    return 0;
}

//引用即变量的替身,相当于操作传入的变量是一样的
int sequenceSearch2(SSTable &table, Element &key) {
    if (table.length == 0) {
        return -1;
    }
    Element *element = table.element;
    int i = 0;
    while (i < table.length) {
        //结构体直接比较需要运算符重载
        if (key == element[i]) {
            return i;
        }
        i++;
    }
    if (i == table.length) {
        return -1;
    }
    return 0;
}